﻿using Common.AspNetCore;
using Common.Infrastructure;
using Log.Api.Extensions;
using Log.Api.LogModels;
using Log.Api.QueryModel;
using Log.Api.Service;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Threading.Tasks;

namespace Log.Api.Controllers
{
    [Route("api/WorkWipTestParam")]
    public class WorkWipTestParamController : ApiController
    {
        private WorkWipTestParamService workWipTestService;

        public WorkWipTestParamController(IServiceProvider provider, WorkWipTestParamService context)
            : base(provider)
        {
            this.workWipTestService = context;
        }

        /// <summary>
        /// 新增或者修改
        /// </summary>
        /// <param name="sysLogModel"></param>
        [HttpPost("AddModel")]
        public async Task<ApiActionResult<bool>> AddModel([FromBody] WorkWipTestParamModel sysLogModel)
        {
            return await SafeExecuteAsync<bool>(async (param) =>
            {
                return await workWipTestService.InsertAsync(sysLogModel);
            });
        }

        /// <summary>
        /// 删除日志
        /// </summary>
        /// <param name="id"></param>
        [HttpPost("Delete")]
        public ApiActionResult<bool> Delete([FromBody] WorkWipTestParamQuery query)
        {
            return SafeExecute<bool>((param) =>
            {
                return workWipTestService.DeleteByQuery(query);
            });
        }

        /// <summary>
        /// 分页获取
        /// </summary>
        /// <param name="parameter"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpPost("GetOrderPage")]
        public ApiActionResult<ApiPagedResult<WorkWipTestParamModel>> GetOrderPage([FromBody] WorkWipTestParamQuery query)
        {
            return SafeExecute<ApiPagedResult<WorkWipTestParamModel>>((param) =>
            {
                ApiPagedResult<WorkWipTestParamModel> ret = new ApiPagedResult<WorkWipTestParamModel>();
                if (query.BeginTime == null)
                {
                    query.BeginTime = DateTime.Now.AddDays(-7).Date;
                }

                if (query.EndTime == null)
                {
                    query.EndTime = DateTime.Now.AddDays(1).Date;
                }

                query.BeginTime = ElasticClientExtension.GetDateForEs(query.BeginTime.Value);
                query.EndTime = ElasticClientExtension.GetDateForEs(query.EndTime.Value);
                ret.Data = workWipTestService.GetWorkWipTests(query);
                var TotalCount = (int)workWipTestService.GetTotalCount(query);
                ret.TotalCount = TotalCount < 0 ? 0 : TotalCount;
                return ret;
            });
        }
    }
}
